#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e5 + 5;

int n, m;
int f[N];
ll w[N];
int rt[N], deg[N];
ll val[N << 1];
int lc[N << 1], rc[N << 1], dis[N << 1];
int newnode(ll v) {
  static int tot = 0;
  ++tot;
  lc[tot] = rc[tot] = dis[tot] = 0;
  val[tot] = v;
  return tot;
}
int merge(int x, int y) {
  if (!x || !y) return x + y;
  if (val[x] < val[y]) swap(x, y);
  rc[x] = merge(rc[x], y);
  if (dis[lc[x]] < dis[rc[x]]) swap(lc[x], rc[x]);
  dis[x] = dis[rc[x]] + 1;
  return x;
}
void pop(int& rt) { rt = merge(lc[rt], rc[rt]); }
ll ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  dis[0] = -1;
  cin >> n >> m;
  rep(i, 2, n + m) cin >> f[i] >> w[i], ans += w[i], deg[f[i]]++;
  per(i, n + m, n + 1) {
    int x = newnode(w[i]), y = newnode(w[i]);
    rt[f[i]] = merge(rt[f[i]], merge(x, y));
  }
  per(i, n, 2) {
    while (deg[i] > 1) deg[i]--, pop(rt[i]);
    ll r = val[rt[i]];
    pop(rt[i]);
    ll l = val[rt[i]];
    pop(rt[i]);
    int x = newnode(l + w[i]), y = newnode(r + w[i]);
    rt[f[i]] = merge(rt[f[i]], merge(rt[i], merge(x, y)));
  }
  while (deg[1] > 0) deg[1]--, pop(rt[1]);
  while (rt[1]) ans -= val[rt[1]], pop(rt[1]);
  cout << ans;
  return 0;
}